package com.assessment.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){
        // 设置要显示的swagger环境
        Profiles profiles  = Profiles.of("dev");
        // 获取项目环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 配置
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.assessment.controller"))//选择要扫描的包
                // .apis(RequestHandlerSelectors.any())//扫描全部
                // .apis(RequestHandlerSelectors.none())//不扫描
                // .apis(RequestHandlerSelectors.withClassAnnotation(Configuration.class))//扫描类上的有@Configuration注解
                // .apis(RequestHandlerSelectors.withMethodAnnotation(ResponseBody.class))//扫描方法上的有@Configuration注解
                .paths(PathSelectors.ant("/**"))// 扫描com.learn.controller包下"/demo/**"路径下的接口
                .build()
                // 启动配置
                .enable(flag)//根据环境是否启动swagger
                ;
    }
    // 配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        Contact DEFAULT_CONTACT = new Contact("鑫十一", "http://www.baidu.com", "111@qq.com");
        return new ApiInfo(
            "swagger 接口文档",
            "Api文档描述",
            "1.0",
            "http://www.baidu.com\"",
            DEFAULT_CONTACT,
            "Apache 2.0",
            "http://www.apache.org/licenses/LICENSE-2.0",
            new ArrayList());
    }
}
